package com.platform.common.bean;


import com.platform.common.menus.SysConfigEnum;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;

/**
 * describe:平台系统配置
 * 用于控制权限
 * @author Administrator
 * @date 2020/04/30
 * @Time 0:10
 */
@Slf4j
public class SystemConfigBean {

    private SystemConfigBean(){
        if (Inner.BEAN != null){
            throw new RuntimeException("不允许非法访问");
        }
    }

    /**
     * 角色继承关系
     */
    @Getter
    private String roleInheritance;

    /**
     * 权限颗粒
     */
    @Getter
    private String permissionGrain;

    /**
     * 职责分离
     */
    @Getter
    private String separationOfDuty;

    private static SystemConfigBean systemConfig = null;

    /**
     * 在系统启动的时候就进行设置
     * com.platform.system.config.SystemConfig在进行注入
     * @param r 继承关系
     * @param p 权限颗粒
     * @param s 职责分离
     */
    public static void setConfig(@NotBlank SysConfigEnum.RoleInheritance r,
                                 @NotBlank SysConfigEnum.PermissionGrain p,
                                 @NotBlank SysConfigEnum.SeparationOfDuty s){
        if (null != systemConfig){
            log.error("已经被设置了，不能重复设置");
            return;
        }
        log.info("注入系统设置参数");
        systemConfig = Inner.BEAN;
        systemConfig.permissionGrain = p.getValue();
        systemConfig.roleInheritance = r.getValue();
        systemConfig.separationOfDuty = s.getValue();
        log.info("注入系统设置参数成功");
    }

    public static SystemConfigBean getInstance(){
        return Inner.BEAN;
    }

    private static class Inner{
        private static final SystemConfigBean BEAN = new SystemConfigBean();
    }
}
